﻿using System;
using Lidgren.Network;
using Paypayes.Aelda.Core.SimulationEvents;

namespace Paypayes.Aelda.Core
{
    public class EventSerializer// : IEventPacker
    {
/*        public void Pack(SimulationEvent sevt, ref Object buffer)
        {
            NetOutgoingMessage msg = (NetOutgoingMessage)buffer;
            msg.Write((Byte)sevt.EventType);
            msg.Write((Byte)sevt.Channel);
            msg.Write(sevt.SourceId);
            msg.Write(sevt.TargetId);

            switch (sevt.EventType)
            {
                case EventType.Login:
                    msg.Write(((String)sevt.Arguments[0]));
                    msg.Write(((String)sevt.Arguments[1]));
                    break;
                case EventType.LoginResult:
                    msg.Write(((Boolean)sevt.Arguments[0])); // granted
                    break;
                case EventType.UserAttached:
                    msg.Write(((Boolean)sevt.Arguments[0]));
                    msg.Write(((UInt64)sevt.Arguments[1]));
                    msg.Write(((UInt32)sevt.Arguments[2]));
                    msg.Write(((UInt32)sevt.Arguments[3]));
                    break;
                case EventType.ActorMoved:
                    msg.Write(Convert.ToUInt64(sevt.Arguments[0])); // startTime
                    msg.Write(Convert.ToByte(sevt.Arguments[1])); // direction
                    break;
                case EventType.ActorMovedAck:
                    msg.Write(((Boolean)sevt.Arguments[0])); // accepted
                    break;
                case EventType.ActorStoppedMoving:
                    msg.Write(Convert.ToUInt64(sevt.Arguments[0])); // time
                    break;

            }
        }

        public SimulationEvent Unpack(Object buffer)
        {
            NetBuffer msg = (NetBuffer)buffer;
            EventType type = (EventType)msg.ReadByte();
            EventChannel channel = (EventChannel)msg.ReadByte();
            UInt64 sourceId = msg.ReadUInt64();
            UInt64 targetId = msg.ReadUInt64();
            SimulationEvent evt = null;

            switch (type)
            {
                case EventType.Login:
                    evt = new Login(msg.ReadString(), msg.ReadString());
                    break;
                case EventType.LoginResult:
                    evt = new LoginResult(msg.ReadBoolean());
                    break;
                case EventType.UserAttached:
                    evt = new UserAttached(msg.ReadBoolean(), msg.ReadUInt64(), msg.ReadUInt32(), msg.ReadUInt32());
                    break;
                case EventType.ActorMoved:
                    evt = new ActorMoved(msg.ReadUInt64(), (MovementDirection)msg.ReadByte());
                    break;
                case EventType.ActorMovedAck:
                    evt = new ActorMovedAck(msg.ReadBoolean());
                    break;
                case EventType.ActorStoppedMoving:
                    evt = new ActorStoppedMoving((UInt64)msg.ReadUInt64());
                    break;

            }

            return evt;
        }
 */
    }
}

